﻿/** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/

package player {
	
	public class WebTVVideo {

		public var link:String;
		public var bitrate:Number;
		public var bytePosition:Number = 0;
		public var isDynamic:Boolean;
		private var _serverSeekAvailable:Boolean;
		private var _isStreaming:Boolean;
		private var _streamServerUrl:String;
		private var _streamName:String;
		public var isAdvertisement:Boolean;

		public function WebTVVideo(link:String, bitrate:Number, bytePosition:Number=0, serverSeekAvailable:Boolean=false, isDynamic:Boolean=false, isAdvertisement:Boolean=false) {
			this.link = link;
			this.bitrate = bitrate;
			this.bytePosition = bytePosition;
			this._serverSeekAvailable = serverSeekAvailable;
			this.isDynamic = isDynamic;
			this.isAdvertisement = isAdvertisement;
			
			_initIsStreaming();
		}
		
		private function _initIsStreaming():void {
			_isStreaming = this.link.indexOf("rtmp") >= 0;
		}
		
		public function get isStreaming():Boolean{
			return _isStreaming;
		}
		
		/**
		 * Parses stream server url of the link
		 *
		 * Notice! This is needed only for streaming videos. 
		 * This shouldn't be needed for progressive videos
		 */
		public function get streamServerUrl():String{
			if(_streamServerUrl == null){
				_streamServerUrl = this.link.substr(0, this.link.lastIndexOf("/") + 1);
			}
			return _streamServerUrl;
		}
		
		public function get streamName():String{
			/*
			if(_streamName == null){
				var fileName:String = link.substr(link.lastIndexOf("/") + 1, link.length);
				var type:String = link.substr(link.lastIndexOf(".") + 1, link.length);
				var fileNameWithoutType:String = fileName.substr(0, fileName.lastIndexOf("."));
				
				_streamName = type + ":" + fileNameWithoutType;
			}
			return _streamName;
			*/
			
			if(_streamName == null){
				var slashIndex:int = link.lastIndexOf("/");
				var fileName:String = link.substr(slashIndex + 1, link.length);
				
				var dotIndex:int = fileName.lastIndexOf(".");
				
				if (dotIndex >= 0 && fileName.length - dotIndex - 1 < 4) {
					var type:String = fileName.substr(dotIndex + 1, fileName.length);
					var fileNameWithoutType:String = fileName.substr(0, dotIndex);
					_streamName = type + ":" + fileNameWithoutType;
				} else {
					_streamName = "flv:" + fileName;
				}
			}
			return _streamName;
		}
		
		public function get serverSeekAvailable():Boolean{
			if(isStreaming){
				return false;
			} else {
				return _serverSeekAvailable;
			}
		}

	}
	
}
